package fr.labri.harmony.core.model;

import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Action extends SourceElement {

	
	@Enumerated
    private ActionKind kind;

    @ManyToOne
    @JoinColumn(name="eventId", nullable=false)
    private Event event;

    @ManyToOne
    @JoinColumn(name="parentEventId", nullable=true)
    private Event parentEvent;

    @ManyToOne
    @JoinColumn(name="itemId", nullable=false)
    private Item item;

	public Action() {
        super();
    }

    public Action(Item item, ActionKind kind, Event event, Event parentEvent, Source source) {
        this();
        this.item = item;
        this.kind = kind;
        this.event = event;
        this.parentEvent = parentEvent;
        setSource(source);
    }
    

    public ActionKind getKind() {
        return kind;
    }

    public void setKind(ActionKind kind) {
        this.kind = kind;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public Event getParentEvent() {
        return parentEvent;
    }

    public void setParentEvent(Event parentEvent) {
        this.parentEvent = parentEvent;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }
    
    public String toString() {
		return kind + " " + item;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((event.getNativeId() == null) ? 0 : event.getNativeId().hashCode());
		result = prime * result + ((item.getNativeId() == null) ? 0 : item.getNativeId().hashCode());
		result = prime * result + ((kind == null) ? 0 : kind.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Action other = (Action) obj;
		if (event.getNativeId() == null) {
			if (other.event.getNativeId() != null)
				return false;
		} else if (!event.getNativeId().equals(other.event.getNativeId()))
			return false;
		if (item.getNativeId() == null) {
			if (other.item.getNativeId() != null)
				return false;
		} else if (!item.getNativeId().equals(other.item))
			return false;
		if (kind != other.kind)
			return false;
		return true;
	}

}
